package com.umeox.watch.moto.chat.adapter

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.umeox.moto.common.rvadapter.MultiItemTypeAdapter
import com.umeox.moto.common.rvadapter.ViewHolder
import com.umeox.moto.common.rvadapter.wrapper.ItemViewDelegate
import com.umeox.moto.common.utils.DateUtils
import com.umeox.moto.watch.themes.widget.RoundedImageView
import com.umeox.watch.moto.chat.R
import com.umeox.watch.moto.chat.base.GlideApp
import com.umeox.watch.moto.chat.db.ContactInfo
import com.umeox.watch.moto.chat.utils.Utility
import java.util.*

/**
 * Created by Rambo 2021/8/20
 * Describe:、
 */
class ChatContactsAdapter(
    context: Context, contactInfos: List<ContactInfo?>?
) : MultiItemTypeAdapter<ContactInfo>(context, contactInfos) {

    init {
        addItemViewDelegate(ContactInfoDelegate())
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }


    private inner class ContactInfoDelegate : ItemViewDelegate<ContactInfo?> {

        override fun getItemViewLayoutId(): Int {
            return R.layout.item_contact
        }

        override fun convert(viewHolder: ViewHolder?, contactInfo: ContactInfo?, position: Int) {
            viewHolder!!.setText(R.id.tvName, contactInfo!!.name)
            val unReadIv:ImageView = viewHolder.getView(R.id.iv_unread)
            val lastContactTime: TextView = viewHolder.getView(R.id.time)
            val text: String = Utility.sessionTimeFormat(mContext, contactInfo.lastContactTime)
            if (TextUtils.isEmpty(text)) {
                lastContactTime.visibility = View.GONE
            } else {
                lastContactTime.visibility = View.VISIBLE
                lastContactTime.text = text
            }

            unReadIv.visibility = if (contactInfo.missCount > 0)  View.VISIBLE else View.GONE
            val avatarIv: RoundedImageView = viewHolder.getView(R.id.ivAvatar)
//            if (contactInfo.isAdmin) {
//                avatarIv.borderWidth = 4.0f
//                avatarIv.borderColor = mContext.resources.getColor(R.color.light_navigationColor)
//            } else {
//                avatarIv.borderWidth = 0.0f
//            }
            if (TextUtils.isEmpty(contactInfo.avatar)) {
                GlideApp.with(mContext)
                    .load(if (contactInfo.isGroup) R.drawable.default_group_avatar else R.drawable.default_avatar)
                    .apply(RequestOptions.bitmapTransform(CircleCrop()))
                    .into(avatarIv)
            } else {
                GlideApp.with(mContext)
                    .load(contactInfo.avatar)
                    .apply(RequestOptions.bitmapTransform(CircleCrop()))
                    .into(avatarIv)
            }
        }


        override fun isForViewType(contactInfo: ContactInfo?, position: Int): Boolean {
            return true
        }


        private fun formatLastContact(lastContactTime: Long): String {
            if (lastContactTime == 0L || System.currentTimeMillis() - lastContactTime < 0) {
                return ""
            }
            val start = Calendar.getInstance()
            start.timeInMillis = lastContactTime
            val end = Calendar.getInstance()
            end.timeInMillis = System.currentTimeMillis()
            return if (sameDate(start, end)) {
                String.format(Locale.getDefault(), "%tR", lastContactTime)
            } else {
                if (sameYear(start, end)) {
                    DateUtils.formatDateTime(lastContactTime, "MM/dd")
                } else {
                    DateUtils.formatDateTime(lastContactTime, "yyyy/MM/dd")
                }
            }
        }

        private fun sameYear(start: Calendar, end: Calendar): Boolean {
            return start[Calendar.YEAR] == end[Calendar.YEAR]
        }

        private fun sameMonth(start: Calendar, end: Calendar): Boolean {
            return start[Calendar.MONTH] == end[Calendar.MONTH]
        }

        private fun sameDate(start: Calendar, end: Calendar): Boolean {
            return sameYear(start, end) && sameMonth(
                start,
                end
            ) && start[Calendar.DAY_OF_MONTH] == end[Calendar.DAY_OF_MONTH]
        }
    }
}